/**
 * Result.java [V 1.0.0]
 * classes : com.xiaocq.iwordkiller.Result
 * xiaocq Create at 2012-4-12 下午8:09:13
 */
package com.xiaocq.iwordkiller;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.database.Cursor;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.xiaocq.iwordkiller.db.PrepareCursorHelper;

/**
 * com.xiaocq.iwordkiller.Result
 * @author Mystery
 * Create at 2012-4-12 下午8:09:13
 */
public class ResultShow extends Activity{
	private static final String TAG = "Result";
	
	private Param mParam;
	private Param.StatisticParam sParam;
	private float perWordTimeCost;
	private float perWordRightTimeCost;
	private float hitRate;
	private int rightCounts;
	
	private Button backToMenu;
	private TextView displayTime,disPlayCounts,disPlayRate,disPlayLevel;
	private ListView wrongList;
	private LoadTask mTask;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		mParam = new Param(this);
		sParam = new Param.StatisticParam(this);
		sParam.load();
		
		Intent intent = getIntent();
		mParam.loadFromIntent(intent);
		Log.d(TAG, mParam.toString());
		
		rightCounts = mParam.testCounts - mParam.getWrongCounts();
		perWordRightTimeCost = (float) ((rightCounts == 0) ? mParam.totalTestTime : (mParam.totalTestTime * 1.0 / rightCounts)) / 1000;
		perWordTimeCost = (float) (mParam.totalTestTime * 1.0 / mParam.testCounts) / 1000;
		hitRate = (float) (rightCounts == 0 ? 0.00 : rightCounts * 1.0 / mParam.testCounts) * 100;
		
		sParam.addParam(mParam, perWordRightTimeCost);
		sParam.save();
		
		setContentView(R.layout.result);
		
		backToMenu = (Button) findViewById(R.id.result_show_backToMenu);
		displayTime = (TextView) findViewById(R.id.result_show_timeCost);
		disPlayCounts = (TextView) findViewById(R.id.result_show_wordsCount);
		disPlayRate = (TextView) findViewById(R.id.result_show_hitRate);
		disPlayLevel = (TextView) findViewById(R.id.result_show_rate);
		wrongList = (ListView) findViewById(R.id.result_show_wrongWordList);
		
		mTask = new LoadTask();
		mTask.execute();
		
		backToMenu.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				finish();
			}
		});
		
		displayTime.setText(getCombineTimeText());
		disPlayCounts.setText(getCombineCountText());
		disPlayRate.setText("命中率：" + String.format("%1$-4.2f", hitRate) + "%");
		disPlayLevel.setText(getRankText());
	}

	
	private class LoadTask extends AsyncTask<Void, Void, Void>{
		private ProgressDialog progressDialog;
		private PrepareCursorHelper helper;
		private Cursor mData;
		private CostumAdapter adapter;
		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			
			progressDialog = new ProgressDialog(ResultShow.this);
			progressDialog.setMessage(getText(R.string.tip_loading));
			progressDialog.setCancelable(false);
			progressDialog.show();
			
			helper = new PrepareCursorHelper(ResultShow.this);
			helper.open();
		}

		@Override
		protected Void doInBackground(Void... params) {
			mData = helper.getWrongDataOfTest(mParam.finishTime);
			return null;
		}
		
		@Override
		protected void onPostExecute(Void result) {
			super.onPostExecute(result);
			adapter = new CostumAdapter(ResultShow.this, mData);
			wrongList.setAdapter(adapter);
			helper.close();
			progressDialog.dismiss();
			
		}
		
	}
	
	private String getCombineTimeText(){
		String result = "耗时：";
		long hour,minute,second;
		second = mParam.totalTestTime / 1000;
		minute = second / 60L;
		second %= 60L;
		hour = minute / 60L;
		minute %= 60L;
		if(hour > 0){
			result += (hour + "小时");
			result += (minute + "分");
		}else if(minute > 0){
			result += (minute + "分");
		}
		result += (second + "秒");
		result += " 速度：";
		result += (String.format("%1$-4.2f", perWordTimeCost) + "秒/个");
		
		return result;
	}
	
	private String getCombineCountText(){
		String result = "共计 ";
		result += mParam.testCounts + "题";
		result += " 对 " + rightCounts;
		result += " 错 " + mParam.getWrongCounts();
		
		return result;
	}
	
	private String getRankText(){
		if(hitRate > 99.99f){
			return "S";
		}
		if(hitRate > 95.00f){
			return "A+";
		}
		if(hitRate > 90.00f){
			return "A";
		}
		if(hitRate > 85.00f){
			return "B+";
		}
		if(hitRate > 80.00f){
			return "B";
		}
		if(hitRate > 75.00f){
			return "C+";
		}
		if(hitRate > 70.00f){
			return "C";
		}
		if(hitRate > 65.00f){
			return "D";
		}
		if(hitRate > 60.00f){
			return "E";
		}
		
		return "F";
	}
}
